import pandas as pd
import requests


def gaodexy(address):
    par = {'address': address, 'key': '6c11198d7b9770404ee7cbcc28b200b5'}  ###需要自己在高德中申请密钥
    base = 'http://restapi.amap.com/v3/geocode/geo'
    response = requests.get(base, par)
    answer = response.json()
    print("answer:", answer['status'], "--", answer['infocode'])
    if answer['status'] != '1':
        return '无结果','无结果'
    else:
        GPS = answer['geocodes'][0]['location'].split(",")
        print("gaodexy--address:",address,GPS)
        return GPS[0], GPS[1]


def gaodedidian(address):
    par = {'address': address, 'key': '6c11198d7b9770404ee7cbcc28b200b5'}
    base = 'http://restapi.amap.com/v3/geocode/geo'
    response = requests.get(base, par)
    answer = response.json()
    print("answer:", answer['status'], "--", answer['infocode'])
    if answer['status'] != '1':
        return '无结果'
    else:
        GPS = answer['geocodes'][0]['formatted_address'].split(",")
        print("gaodedidian--address:",address,GPS)
        return GPS[0]

# 读取文件
df = pd.read_excel('新.xlsx')


# 根据地址查询
df['经纬度'] = df['地址'].apply(lambda x: gaodexy(x))
df['行政区'] = df['地址'].apply(lambda x: gaodedidian(x))

# 拆分经纬度到单独的列
df[['经度', '纬度']] = pd.DataFrame(df['经纬度'].tolist(), index=df.index)

# 可以打印输出整个DataFrame来查看结果
# print(df)

df.to_excel('file2.xlsx')
print("over")